<template>
  <div class="app-container" style="padding: 10px">
    <el-card class="box-card" shadow="false">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="接口名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入接口名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="typeList" border>
        <el-table-column
          label="接口名称"
          align="left"
          prop="name"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="接口地址/页面编码"
          align="left"
          prop="url"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="请求方式"
          align="left"
          prop="requestMethod"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="显示字段"
          align="left"
          prop="displayField"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="储存字段"
          align="left"
          prop="saveField"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="备注"
          align="left"
          prop="remark"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-order"
              @click="handleUpdate(scope.row, true)"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color: #f56c6c"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
          <el-form-item label="接口类型" prop="app">
            <el-radio-group v-model="form.app" :disabled="isDetail" @change="handleAppChange">
              <el-radio label="1">数据接口</el-radio>
              <el-radio label="2">API接口</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="接口名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入接口名称"
              :disabled="isDetail"
            />
          </el-form-item>

          <el-form-item :label="form.app === '1' ? '页面编码' : '接口地址'" prop="url">
            <el-input
              v-model="form.url"
              :placeholder="`请输入${form.app === '1' ? '页面编码' : '接口地址'}`"
              :disabled="isDetail"
            />
          </el-form-item>
          <el-form-item label="请求方式" prop="requestMethod">
            <el-radio-group
              v-model="form.requestMethod"
              :disabled="isDetail || form.app === '1'"
            >
              <el-radio label="get">get</el-radio>
              <el-radio label="post">post</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="储存字段" prop="saveField">
            <el-input
              v-model="form.saveField"
              placeholder="请输入储存字段"
              :disabled="isDetail"
            />
          </el-form-item>
          <el-form-item label="显示字段" prop="displayField">
            <el-input
              v-model="form.displayField"
              placeholder="请输入显示字段"
              :disabled="isDetail"
            />
          </el-form-item>
          <el-form-item label="请求参数">
            <div
              class="requestParams"
              v-for="(item, index) in form.requestParam"
              :key="index"
              style="margin-bottom: 10px"
            >
              <el-input
                v-model="item.label"
                placeholder="请输入key"
                :disabled="isDetail"
              />
              <el-input
                v-model="item.value"
                placeholder="请输入value"
                :disabled="isDetail"
              />
              <i
                class="el-icon-circle-plus-outline"
                style="color: #409eff; font-size: 25px; cursor: pointer"
                @click="handleAddItem('1', item, index)"
                v-if="index === 0"
              ></i>
              <i
                class="el-icon-remove-outline"
                style="color: #f56c6c; font-size: 25px; cursor: pointer"
                @click="handleAddItem('2', item, index)"
                v-else
              ></i>
            </div>
          </el-form-item>
          <el-form-item label="返回数据路径" :prop="form.app === '1' ? undefined : 'returnDataPath'">
            <span slot="label">
              返回数据路径
              <el-tooltip
                content="如:data、data.rows、data.records"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
            <el-input
              type="textarea"
              v-model="form.returnDataPath"
              placeholder="请输入返回数据路径"
              :disabled="isDetail || form.app === '1'"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入备注"
              :disabled="isDetail"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!isDetail"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, save, getOrDelete } from "@/api/system/customInterface";
export default {
  name: "Dict",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      editRow: undefined,
      isDetail: false,
      // 表单参数
      form: {
        requestParam: [{}],
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "接口名称不能为空", trigger: "blur" },
        ],
        url: [{ required: true, message: "接口地址不能为空", trigger: "blur" }],
        saveField: [
          { required: true, message: "储存字段不能为空", trigger: "blur" },
        ],
        displayField: [
          { required: true, message: "显示字段不能为空", trigger: "blur" },
        ],
        returnDataPath: [
          { required: true, message: "返回数据路径不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAddItem(type, row, index) {
      if (this.isDetail) return;
      if (type === "1") {
        this.form.requestParam.push({});
      } else {
        this.form.requestParam = this.form.requestParam?.filter(
          (x, i) => i !== index
        );
      }
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then((response) => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleAppChange(val){
      if(val === '1'){
        this.form.requestParam = [{label:'page',value:1},{label:'perPage',value:9999}]
      }else{
        this.form.requestParam = [{}]
      }
    },
    // 表单重置
    reset() {
      const _this = this
      this.form = {
        requestMethod: "post",
        remark: undefined,
        returnDataPath: undefined,
        name: undefined,
        url: undefined,
        saveField: undefined,
        displayField: undefined,
        app: "1",
        requestParam:[{label:'page',value:1},{label:'perPage',value:9999}]
      };
      // this.resetForm("form");
      this.isDetail = false;
      this.editRow = undefined;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.reset();
      this.editRow = row;
      this.isDetail = type;
      getOrDelete(row.id, "get").then((response) => {
        const data = response.data;
        if (!!data.requestParam) {
          data.requestParam = JSON.parse(data.requestParam);
        }
        this.form = { ...data };
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form));
          params.requestParam = JSON.stringify(params.requestParam);
          const res = await save(params, this.editRow ? "put" : "post");
          if (res.code === 200) {
            this.$message.success("操作成功");
            this.open = false;
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除该条数据？")
        .then(function () {
          return getOrDelete(row.id, "delete");
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>

.requestParams {
  display: flex;
  align-items: center;
  ::v-deep .el-input--medium {
    margin-right: 12px;
  }
}
</style>
